package engine.systems.spatial.edge
{
	import engine.classes.enumerations.SystemPriority;
	import engine.systems.update.GameSystem;
	
	import flash.geom.Rectangle;
	
	import org.flintparticles.common.displayObjects.Rect;
	
	public class EdgeSystem extends GameSystem
	{
		public function EdgeSystem()
		{
			super(EdgeNode, updateNode, null, nodeRemoved);
			
			this.priority = SystemPriority.EDGE;
		}
		
		private function updateNode(node:EdgeNode, time:Number):void
		{
			var edge:Edge = node.edge;
			
			var scaleX:Number = node.render.display.scaleX;
			var scaleY:Number = node.render.display.scaleY;
			
			var absScaleX:Number = Math.abs(scaleX);
			var absScaleY:Number = Math.abs(scaleY);
			
			if(scaleX >= 0)
			{
				edge._scaledLeft 	= edge.left * absScaleX;
				edge._scaledRight 	= edge.right * absScaleX;
			}
			else
			{
				edge._scaledLeft 	= edge.right * absScaleX;
				edge._scaledRight 	= edge.left * absScaleX;
			}
			
			if(scaleY >= 0)
			{
				edge._scaledTop 	= edge.top * absScaleX;
				edge._scaledBottom 	= edge.bottom * absScaleX;
			}
			else
			{
				edge._scaledTop 	= edge.bottom * absScaleX;
				edge._scaledBottom 	= edge.top * absScaleX;
			}
		}
		
		private function nodeRemoved(node:EdgeNode):void
		{
			var edge:Edge = node.edge;
			
			edge._scaledLeft 	= edge.left;
			edge._scaledTop 	= edge.top;
			edge._scaledRight 	= edge.right;
			edge._scaledBottom 	= edge.bottom;
		}
	}
}